<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of sealchat
 *
 * @author Samuel
 */
class sealchat_Controller Extends Controller {
	
	private $inc;
	private $platformlist;
	private $areaList;	//区号列表 
	private $sealPlayerDesribeList; //封角色描述列表
	private $accountList;
	
	function _init() {
        check_level(50, segment(0), segment(1), segment(2, 'index'));
        
        $sql = "SELECT * FROM {pre}areasample ";
	    $this->db->query($sql);
	    //echo "sql = $sql";
	    while ($row = $this->db->get_array("default")) 
	    {
	    	//print_r($row);
	    	$this->areaList[$row['id']]['name'] = $row['id']."-".$row['name'];
	        $this->areaList[$row['id']]['host'] = $row['host'];
	    }
	    if(0 == count($this->areaList))
	    {
	    	echo "<script>alert('" . translate('C_ADD_AREA') . "');</script>";
	    	return;
	    }
	    //echo "hello";
	    //var_dump($this->areaList);
		//exit() ;
		
	    //描述
        $sql = "SELECT *  FROM gm_sealplayer ";
        $this->db->query($sql);
    	while ($n = $this->db->get_array('default')) 
    	{
            $this->sealPlayerDesribeList[$n['area']][$n['roleid']][$n['sealtype']]=array('description' => $n['description'], 'platform' => $n['platform'], 'account'=>$n['account'], 'inserttime'=>$n['inserttime'], 'operator'=>$n['operator']);   
        } 
        //读入account表
        $tmp_connect = $this->load->database('center_db') or die("load failed!".mysql_error());
        $tmpsql = "SELECT id, name FROM account ";
        $tmp_connect->query($tmpsql) or die("query failed!".mysql_error());
        while($accountrow = $tmp_connect->get_array()){
        	$this->accountList[$accountrow['id']] = $accountrow['name'];
        }
        
        $this->inc = $this->config['inc'];
        $this->platformlist = $this->inc['platform_list'];
    }
	
	
    function index() {
    	$areahostArray = get_area_list_option();
    	//print_r ($_SESSION['guolan']);
    	$selectedArea = $_SESSION['area_selected_id'];
    	$this->view->assign("arealist", $this->form->select("area_selected_id", $areahostArray, $selectedArea, array('id' => 'area_selected_id')));
        $this->view->display("seal/sealchat/index");
    }

    function flexigrid() {
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();

        $area_host = !empty($_SESSION['area_selected_host']) ? $_SESSION['area_selected_host'] : '';
        $selectedArea = $_SESSION['area_selected_id'];
        $strResult = vpost($area_host . "action=3");

        $row = json_decode($strResult, true);
		//print_r($row);
		//exit(0);

        foreach ($row as $value) {
            if ($value['chat_end_time'] <= time())
                continue;
            $value['chat_end_time'] = get_datetime($value['chat_end_time']);
            
            $value['sealtype'] = 2; //固定为2
            
            $tmparray = $this->sealPlayerDesribeList[$selectedArea][$value['role_id']][$value['sealtype']];
            $value['description'] = $tmparray['description'];
            $value['platform'] = $this->platformlist[$tmparray['platform']];
            $value['account'] = $tmparray['account'];
            $value['inserttime'] = $tmparray['inserttime'];
            $value['operator'] = $tmparray['operator'];
            $value['area'] = $this->areaList[$selectedArea]['name'];
            
            $rows[] = array('id' => $value['role_id'], 'cell' => $value);
        }
        //print_r($rows);
        //exit(0);
        $total = count($rows);
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $rows
        );
        //print_r($this->areaList);
        echo json_encode($result);
    }

    function add() {
        if (is_submit()) {
            $rowdata = $_POST['row'];
            //print_r($rowdata);
            //exit(0);
            $url = $this->areaList[$rowdata['area']]['host'];
            //unset($rowdata['area']);
            $str = array_to_stringparam($rowdata);
            $url .=  $str . "action=2&seal_action=2";
			//echo "url = $url";
            //exit(0);
			$strResult = vpost($url);
			//$strResult == true ? print_r("+") : print_r("-");
			//exit(0);

            $result = 0;
            //操作失败或者连接失败
            if ($strResult != '0' || $strResult === FALSE)
            {
                $result = 1;
            }
            else
            {
                insert_log(translate('MENU_SEAL_SEALCHAT'),translate('ADD'), array("name" => $rowdata['rolename']));
                
            	if($rowdata['description'] != "")
                {
	                //把描述写入到本地数据库
	                $insertData = array();
	                $insertData['area'] = $rowdata['area'];
	                $insertData['roleid'] = $rowdata['role_id'];
	                $insertData['sealtype'] = 2;	//禁言固定为2
	                $insertData['platform'] = $rowdata['platform'];
	                $insertData['inserttime'] = date('Y-m-d H:i:s', time());
	                $insertData['operator'] = $this->auth->fields['username'];
	                ////获取accountname
	                $accountid = getAccountId($rowdata['area'], $rowdata['role_id']);
	                $accountname = "";
	                foreach ($this->accountList as $k=>$v)
	                {
	                	if ($accountid == $k)
	                	{
	                		$accountname = $v;
	                	}
	                }
	                ////
	                $insertData['account'] = $accountname;
	                //$this->sealPlayerDesribeList[$rowdata['area']][$rowdata['role_id']][$insertData['sealtype']]=$insertData['description']; 
	                
	                $this->db->delete("gm_sealplayer", $insertData);
	                $insertData['description'] = $rowdata['description'];
	                $this->db->insert("gm_sealplayer", $insertData);
	                //$this->db->insert("gm_sealplayer", $insertData, true);
	                //$this->db->update("gm_sealplayer",array('description'=>$rowdata['description']), $insertData);
                }
            }
            cp_tips($result == 0 ? "success" : "error");
        }
        $time = array("600" => translate('TEN_MINUTES'), "1800" => translate('HALF_HOUR'), "3600" => translate('ONE_HOUR'), "7200" => translate('TWO_HOURS'), "86400" => translate('ONE_DAY'));

        $this->view->assign("timelist", $this->form->select("row[seal_time]", $time, '', array('id' => 'seal_time')));
        //$this->view->assign("arealist", $this->form->select("row[area_host]", get_area_list_host(), '', array('id' => 'area_host')));
        //var_dump($this->areaList);
        $this->view->assign("platformlist", $this->form->select("row[platform]", $this->platformlist, '', array('id' => 'platfrom')));
		$this->view->assign("areaList", $this->areaList);
		//exit();
        $this->view->display("seal/sealchat/add");
    }

    function edit() {
        
    }

    function del($id=0) {
        $host = !empty($_SESSION['area_selected_host']) ? $_SESSION['area_selected_host'] : "";

        $strResult = vpost($host . "id=" . $id . "&action=4&seal_action=2");

        $res = 1;
        if ($strResult === FALSE && $strResult != "0")
        {
            $res = 0;
        }
        else
        {
            insert_log(translate('MENU_SEAL_SEALCHAT'),translate('DEL'), $id);
             $this->db->delete("gm_sealplayer", array("roleid"=>$id,"sealtype"=>2));
        }

        json_output($res, translate('C_TIPS_ERROR_TEXT'));
    }

}

?>
